﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.tutorial {
    import flash.events.*;
    import org.robotlegs.core.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.events.windows.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.models.fountain.*;
    import net.wooga.pets.models.world.*;

    public class TutorialMediator extends Mediator implements IMediator {

		[Inject]
        public var tutorial:TutorialOverlay;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var tutorialModel:TutorialModel;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var sunModel:SunModel;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
		[Inject]
        public var world:WorldModel;
		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var sessionModel:SessionModel;
		[Inject]
        public var symbolModel:SymbolModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var friendsModel:FriendsModel;
		[Inject]
        public var keyboardInput:KeyboardInput;
		[Inject]
        public var gameViewModel:GameViewModel;
        protected var _actualMappedEvent:String;
        protected var _nextTutorialState:int;
        protected var _triggerTimer:Timer;
        protected var _stateToTrigger:int;
        protected var _kitchenBuilt:Boolean;// = false
        private var _lastEvent:Event;

        override public function onRegister():void{
            eventMap.mapListener(eventDispatcher, GameEvent.TUTORIAL_STATE_CHANGE, this.onStateChange);
            eventMap.mapListener(eventDispatcher, KeyboardInputEvent.ON_CHEAT_MODE, this.onRemoveOverlay);
            eventMap.mapListener(eventDispatcher, EventConstants.VISIT_FRIEND, this.onVisitFriends);
            eventMap.mapListener(eventDispatcher, BuildingEvent.FACADE_CHANGED, this.toggleFacade);
            this.onRemoveOverlay();
            this.onStateChange();
        }
        protected function onStateChange(_arg1:GameEvent=null):void{
            if (this.tutorialModel.isInTutorialMode){
                this.disableControls();
                dispatch(new Event(EventConstants.HIDE_COMPUTER_DISPLAY));
            } else {
                this.onRemoveOverlay();
                this.enableControls();
            };
        }
        protected function centerGameView(_arg1:int):void{
            this.gameViewModel.setGameScaleByIndex(_arg1);
            dispatch(new Event(EventConstants.CENTER_GAMEVIEW));
        }
        protected function get isKioskBuild():Boolean{
            var _local1 = !((Arrays.findByAttribute(this.sessionModel.currentRooms, ProtocolConstants.ROOM_TYPE, GameConstants.TYPE_KIOSK) == null));
            return (((_local1) || (this._kitchenBuilt)));
        }
        protected function kioskAlreadyBuilt():void{
            this.onRemoveOverlay();
            this.gameStateModel.state = GameStateModel.STATE_GAME_RUNNING;
            this.tutorialModel.kioskTutorialActive = false;
            this.tutorialModel.kioskTutorialPending = false;
            var _local1:InfoEvent = new InfoEvent(EventConstants.REPORT_PROBLEM, "kitchen is already built");
            dispatch(_local1);
        }
        protected function mapEvent(_arg1:String, _arg2:int):void{
            this._actualMappedEvent = _arg1;
            this._nextTutorialState = _arg2;
            eventMap.mapListener(eventDispatcher, _arg1, this.onMappedEvent);
        }
        protected function onMappedEvent(_arg1:Event):void{
            this._lastEvent = _arg1;
            eventMap.unmapListener(eventDispatcher, this._actualMappedEvent, this.onMappedEvent);
            this.tutorialModel.state = this._nextTutorialState;
        }
        protected function mapTreatmentFinished(_arg1:InfoEvent):void{
            switch (this.tutorialModel.state){
                case TutorialModel.LEVEL1_BOOST_PATIENT:
                    this.tutorialModel.state = TutorialModel.LEVEL1_FINISH_LEVEL1_TUTORIAL;
                    break;
            };
        }
        protected function onBatterieNeeded(_arg1:InfoEvent):void{
            if (_arg1.id == GameConstants.TYPE_BATTERY_ROOM){
                dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.TUTORIAL, {step:"103_treatment_no_battery"}));
            };
        }
        protected function mapClickOnSlotmachine(_arg1:Event):void{
            dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.TUTORIAL, {step:"105_second_slotmachine"}));
            eventMap.unmapListener(eventDispatcher, SlotMachineEvent.START_ROLLING, this.mapClickOnSlotmachine);
        }
        protected function mapRoomCreated(_arg1:InfoEvent):void{
            var _local2:IRoom = (_arg1.info as IRoom);
            if (_local2){
                switch (_local2.type){
                    case GameConstants.TYPE_RECEPTION:
                        this.tutorialModel.state = TutorialModel.LEVEL1_BUILD_WAITING_ROOM;
                        break;
                    case GameConstants.TYPE_WAITING_ROOM:
                        this.tutorialModel.state = TutorialModel.LEVEL1_BUILD_BATTERY_ROOM;
                        break;
                    case GameConstants.TYPE_BATTERY_ROOM:
                        this.tutorialModel.state = TutorialModel.LEVEL1_BUILD_TREATMENT_GENERAL_ROOM;
                        break;
                    case GameConstants.TYPE_TREATMENT_GENERAL:
                        this.tutorialModel.state = TutorialModel.LEVEL1_BUILD_TREATMENT_GENERAL_ROOM_2;
                        break;
                };
            };
        }
        protected function mapCloseCustomizeMenu(_arg1:Event):void{
            eventMap.unmapListener(eventDispatcher, EventConstants.TRY_TO_CANCEL_BUILDMODE, this.mapTryToCancel);
        }
        protected function mapBubbleClosed(_arg1:Event):void{
            switch (this.tutorialModel.state){
                case TutorialModel.ROOMTUTORIAL_START_TUTORIAL:
                    this.triggerNewState(TutorialModel.ROOMTUTORIAL_CLICK_ON_SHOP, 0);
                    break;
                case TutorialModel.KIOSK_START_TUTORIAL:
                    if (this.gameStateModel.isBuildMode){
                        this.triggerNewState(TutorialModel.KIOSK_CHOOSE_ROOM, 1000);
                    } else {
                        this.triggerNewState(TutorialModel.KIOSK_CLICK_ON_SHOP, 1000);
                    };
                    break;
                case TutorialModel.KINDERGARTEN_START_TUTORIAL:
                    if (this.gameStateModel.isBuildMode){
                        this.triggerNewState(TutorialModel.KINDERGARTEN_CHOOSE_ROOM, 1000);
                    } else {
                        this.triggerNewState(TutorialModel.KINDERGARTEN_CLICK_ON_SHOP, 1000);
                    };
                    break;
                case TutorialModel.FOUNTAIN_START_TUTORIAL:
                    if (this.gameStateModel.isBuildMode){
                        this.triggerNewState(TutorialModel.FOUNTAIN_CHOOSE_ROOM, 1000);
                    } else {
                        this.triggerNewState(TutorialModel.FOUNTAIN_CLICK_ON_SHOP, 1000);
                    };
                    break;
            };
        }
        protected function mapDestroyWindow(_arg1:Event):void{
            this.tutorialModel.state = TutorialModel.FOUNTAIN_FINISH_TUTORIAL;
            eventMap.unmapListener(eventDispatcher, WindowCommandTriggerEvent.DESTROY_STANDARD_WINDOW, this.mapDestroyWindow);
            eventMap.unmapListener(eventDispatcher, EventConstants.FOUNTAIN_CLOSED, this.mapDestroyWindow);
        }
        protected function mapTryToCancel(_arg1:Event):void{
            if ((((this.tutorialModel.state > TutorialModel.KIOSK_START_TUTORIAL)) && ((this.tutorialModel.state < TutorialModel.KIOSK_FINISH_TUTORIAL)))){
                this.sunModel.simpleMessage = this.languageModel.getTextByKey("sun_message_buildkiosk");
            } else {
                this.sunModel.simpleMessage = this.languageModel.getTextByKey("sun_message_buildroom");
            };
        }
        protected function triggerNewState(_arg1:int, _arg2:int):void{
            this._triggerTimer = new Timer(_arg2, 1);
            this._triggerTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onTriggerTimerComplete);
            this._triggerTimer.start();
            this._stateToTrigger = _arg1;
        }
        protected function onTriggerTimerComplete(_arg1:TimerEvent):void{
            this._triggerTimer.stop();
            this._triggerTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.onTriggerTimerComplete);
            this.tutorialModel.state = this._stateToTrigger;
        }
        protected function onRemoveOverlay(_arg1:Event=null):void{
            this.tutorial.hideClickCatcher();
            this.tutorial.hideSpotlight();
            this.tutorial.hideFocusArrow();
            this.keyboardInput.keyboardEnabled = true;
            dispatch(new WindowsMainViewUpdateEvent(WindowsMainViewUpdateEvent.REMOVE_ALL_SUN_WINDOWS));
        }
        protected function onVisitFriends(_arg1:InfoEvent):void{
            if (this.userModel.isVisitFriendsMode()){
                this.onRemoveOverlay();
                eventMap.unmapListener(eventDispatcher, GameEvent.TUTORIAL_STATE_CHANGE, this.onStateChange);
            } else {
                eventMap.mapListener(eventDispatcher, GameEvent.TUTORIAL_STATE_CHANGE, this.onStateChange);
            };
        }
        protected function getRoomByType(_arg1:String):IRoom{
            var _local2:String = this.world.getRoomIdsOfType(_arg1)[0];
            var _local3:IRoom = (this.world.getRoomById(_local2) as IRoom);
            return (_local3);
        }
        protected function getRoomSlotByType(_arg1:String):Slot{
            var _local2:IRoom = this.getRoomByType(_arg1);
            var _local3:Slot = this.roomSlotsModel.getSlotById(_local2.slotId);
            return (_local3);
        }
        protected function toggleFacade(_arg1:Event):void{
            this.tutorial.visible = !(this.gameStateModel.isFacadeVisible);
        }
        protected function disableControls():void{
            dispatch(new Event(EventConstants.DISABLE_GAME_VIEW_NAVIGATION));
            this.keyboardInput.keyboardEnabled = false;
        }
        protected function enableControls():void{
            dispatch(new Event(EventConstants.ENABLE_GAME_VIEW_NAVIGATION));
            this.keyboardInput.keyboardEnabled = true;
        }
        public function get lastEvent():Event{
            return (this._lastEvent);
        }
        protected function focusShopButton():void{
            this.tutorial.showFocusArrow(270, 235);
            this.tutorial.showClickBlocker(310, 460, 0.3);
            this.tutorial.showSpotlight(310, 460, 0.5);
        }

    }
}//package net.wooga.pets.views.tutorial 
